package com.qf.springbootitem.config;
import com.qf.springbootitem.intercepterpath.IntercepterPath;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ShiroConfig {

    @Autowired
    IntercepterPath intercepterPath;

    @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition(){

        DefaultShiroFilterChainDefinition definition = new DefaultShiroFilterChainDefinition();

        String[] intercepterPath = this.intercepterPath.getIntercepterPath();

        for (String s : intercepterPath) {

            String[] split = s.split(",");

            definition.addPathDefinition(split[0],split[1]);
        }


       /* //需登录即可以访问
        definition.addPathDefinition("/a.html","authc");

        //需要登录并且验证身份才可以登录
        definition.addPathDefinition("/b.html","authc, roles[admin]");

        // url需要当前用户拥有document:read权限才可以访问
        definition.addPathDefinition("/c.html","perms[document:read]");

        // url无需登录即可访问
        definition.addPathDefinition("/d.html","anon");

        definition.addPathDefinition("/user/login","anon");

        definition.addPathDefinition("/user/register","anon");

        definition.addPathDefinition("/regist.html","anon");

        definition.addPathDefinition("/error","anon");

        //如配置 /home 可以免登陆（remember） cookie直接访问，user代表过滤器的名字
        definition.addPathDefinition("/home.html","user");*/

        /*definition.addPathDefinition("/swagger-ui.html","anon");
        definition.addPathDefinition("/swagger/**","anon");
        definition.addPathDefinition("/v2/**","anon");
        definition.addPathDefinition("/webjars/**","anon");
        definition.addPathDefinition("/swagger-resources/**","anon");
        definition.addPathDefinition("/static/**","anon");*/



        //默认所有url需要登陆才可以访问
        definition.addPathDefinition("/**","authc");

        return definition;
    }


    @Bean
    public Realm realm(){


        CustomRealm customRealm = new CustomRealm();

        //设置密码处理方式

        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();

        hashedCredentialsMatcher.setHashAlgorithmName("md5");

        hashedCredentialsMatcher.setHashIterations(1024);

        customRealm.setCredentialsMatcher(hashedCredentialsMatcher);

        return customRealm;
    }
}
